/******************************************************************************\
    Handle.hpp contains a class of self-closing handle.
    Copyright (C) 2008  Victor Vinogradov (fly3333@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
\******************************************************************************/

#ifndef __HANDLE_HPP
#define __HANDLE_HPP

#include <windows.h>
#include ".\Object.hpp"

class cSubHandle : public cSubObject
{
	HANDLE ObjectHandle;
public:
	cSubHandle( HANDLE object_handle )
		: ObjectHandle( object_handle )
	{
	}
	virtual ~cSubHandle()
	{
		CloseHandle( ObjectHandle );
	}
	HANDLE Handle() const 
	{
		return ObjectHandle;
	}
};

/*class cHandle 
{
	cSubHandle *SubHandle;
public:
	cHandle( HANDLE object_handle )
		: SubHandle( new cSubHandle( object_handle ) )
	{
	}
	cHandle( const cHandle &handle )
		: SubHandle( handle.SubHandle )
	{
		SubHandle->Attach();
	}
	~cHandle()
	{
		SubHandle->Detach();
	}
	cHandle &operator =( const cHandle &handle )
	{
		SubHandle->Detach();
		SubHandle = handle.SubHandle;
		SubHandle->Attach();
		return *this;
	}
	HANDLE Handle()
	{
		return SubHandle->Handle();
	}
};/**/

class cHandle : public cObject< cSubHandle >
{
public:
	bool DuplicateFrom( HANDLE handle, DWORD desired_access, bool inherit_handle, bool is_same_accsess )
	{
		HANDLE new_handle;
		if( !DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &new_handle, desired_access, inherit_handle ? TRUE : FALSE, is_same_accsess ? DUPLICATE_SAME_ACCESS : 0 ) )
			return false;
		cSubHandle *sub_handle;
		try
		{
			sub_handle = new cSubHandle( new_handle );
		}
		catch( ... )
		{
			return false;
		}
		SwitchToSubObject( sub_handle );
		return true;
	}
	void Close()
	{
		SwitchToSubObject( NULL );
	}
	operator HANDLE() const 
	{
		if( GetSubObject() == NULL )
			return INVALID_HANDLE_VALUE;
		return GetSubObject()->Handle();
	}
};

struct sSecurityAttributes : public SECURITY_ATTRIBUTES
{
	sSecurityAttributes( LPVOID security_descriptor, BOOL inherit_handle )
	{
		nLength = sizeof( SECURITY_ATTRIBUTES );  
		lpSecurityDescriptor = security_descriptor;  
		bInheritHandle = inherit_handle;
	}
};

/*class cSubSecurityAttributes : public cSubObject
{
	sSecurityAttributes SecurityAttributes;
public:
	cSubSecurityAttributes( LPVOID security_descriptor, BOOL inherit_handle )
		: SecurityAttributes( security_descriptor, inherit_handle )
	{
	}
	virtual ~cSubSecurityAttributes()
	{
	}
	LPSECURITY_ATTRIBUTES Pointer()
	{
		return &SecurityAttributes;
	}
};

class cSecurityAttributes 
{
	cSubSecurityAttributes *SubSecurityAttributes;
public:
	cSecurityAttributes( LPVOID security_descriptor, BOOL inherit_handle )
		: SubSecurityAttributes( new cSubSecurityAttributes( security_descriptor, inherit_handle ) )
	{
	}
	cSecurityAttributes( const cSecurityAttributes &security_attributes )
		: SubSecurityAttributes( security_attributes.SubSecurityAttributes )
	{
		SubSecurityAttributes->Attach();
	}
	~cSecurityAttributes()
	{
		SubSecurityAttributes->Detach();
	}
	cSecurityAttributes &operator =( const cSecurityAttributes &security_attributes )
	{
		SubSecurityAttributes->Detach();
		SubSecurityAttributes = security_attributes.SubSecurityAttributes;
		SubSecurityAttributes->Attach();
		return *this;
	}
	LPSECURITY_ATTRIBUTES Pointer()
	{
		return SubSecurityAttributes->Pointer();
	}
};/**/

#endif
